#include "menu_ui_logic.h"
#include <QDebug>

MenuUiLogic::MenuUiLogic(QWidget *ui, QObject *parent)
    : QObject(parent), ui(qobject_cast<MenuUi *>(ui)) // 将 QWidget* 转换为 MenuUi*
{
    if (this->ui)
    {
        // 连接按钮组中的按钮点击信号到槽函数
        connect(&this->ui->buttonGroup,
                QOverload<int>::of(&QButtonGroup::idClicked),
                this,
                &MenuUiLogic::onButtonClicked);

        // 连接最小化按钮的点击信号到槽函数
        connect(this->ui->minimizeButton,
                &QPushButton::clicked,
                this,
                &MenuUiLogic::onMinimizeButtonClicked);
    }
    else
    {
        qWarning("MenuUiLogic: The provided QWidget* is not a MenuUi*.");
    }
}

void MenuUiLogic::onButtonClicked(int id)
{
    auto *button = qobject_cast<QPushButton *>(ui->buttonGroup.button(id));

    // 将所有按钮的样式表恢复到初始状态
    for (QAbstractButton *b : ui->buttonGroup.buttons())
    {
        if (b != button)
        {
            b->setStyleSheet("background-color: transparent;" "opacity: 0;""border-radius: 7px;");
        }
    }

    // 更改选中按钮的样式
    if (button)
    {
        button->setStyleSheet("background-color: rgba(0, 0, 0, 0.2);""border-radius: 7px;");
        emit ui->buttonClicked(id); // 触发信号，通知按钮点击
    }
}
void MenuUiLogic::onMinimizeButtonClicked()
{
    // 切换菜单的最小化状态
    static bool isMinimized = false;
    isMinimized = !isMinimized;

    if (isMinimized)
    {       // 最小化
        ui->setMinimumWidth(50);
        ui->setMaximumWidth(50);
        MenuUi::setPushButton(ui->minimizeButton, QString::fromUtf8(":/res/RetroStingySUV.svg"), "");
    }
    else
    {    // 正常状态
        ui->setMinimumWidth(200);
        ui->setMaximumWidth(300);
        ui->titleLabel->setText(tr("     menu"));
        MenuUi::setPushButton(ui->minimizeButton, QString::fromUtf8(":/res/vehicle.svg"), "");
    }
}
MenuUiLogic::~MenuUiLogic() {}
